package edu.strategy.single.hitbybullet;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;

import robocode.AdvancedRobot;
import robocode.Event;
import edu.strategy.single.Single;

public class SavantMicroHitByBullletStrategy extends Single{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	static boolean enemyIsAlive;
	static int currentMax = 0;
	static int oldMax = 0;
	static int distSeg = 0;
	static double hits[][][]=new double[17][17][20];
	final static Rectangle2D.Double fieldRect=new Rectangle2D.Double(0,0,800,600);
	
	public SavantMicroHitByBullletStrategy(AdvancedRobot advancedRobot) {
		super(advancedRobot);
	}
	
	public void prepare(){
		enemyIsAlive = true;
	}

	@Override
	public void work() {
		super.used();
		if(enemyIsAlive){
			hits[currentMax+8][oldMax][distSeg]++;
			chooseVel();
		}
	}

	@Override
	public void work(Event e) {
		work();
	}
	
	public void chooseVel(){
		double maxRating=Double.POSITIVE_INFINITY;
		for(int i=0;i<17;i++){
			double rating=0;
			for(int a=0;a<17;a++){
				for(int b=0;b<17;b++){
					for(int c=0;c<20;c++){
						rating+=((hits[a][b][c]/(Math.abs(i-a)==0?0.5:Math.abs(i-a)))/(Math.abs(oldMax-b)==0?0.5:Math.abs(oldMax-b)))/
						(Math.abs(distSeg-c)==0?0.5:Math.abs(distSeg-c));
					}
				}
			}
			if(rating<maxRating&&fieldRect.contains(project((i-8)*50,super.getAdvancedRobot().getHeadingRadians()))){
				maxRating=rating;
				currentMax=i-8;
			}
		}
	}
	public Point2D.Double project(double dist,double angle){
		return new Point2D.Double(super.getAdvancedRobot().getX()+dist*Math.sin(angle),super.getAdvancedRobot().getY()+dist*Math.cos(angle));
	}

	@Override
	public String toString() {
		return "SavantMicroHitByBullletStrategy";
	}
}
